<?php
namespace app\model;

use think\Config;
use think\Cache;

class Qyweixin
{
    function __construct() {
    }

    //获取access_token-erroe
    //空为获取应用号的access_token；1为获取通讯录的access_token
    public function access_token($type=''){
        Config::load(APP_PATH.'other.php');
        $config = Config::get('qyweixin');//获取企业微信配置
        if($type==1){
            $url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='.$config['corpid'].'&corpsecret='.$config['Txl_secret'];
        }else{
            $url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='.$config['corpid'].'&corpsecret='.$config['Secret'];
        }
        $access_token = http_curl($url);
        if(empty($access_token))return false;
        $access_token = json_decode($access_token);
        return $access_token->access_token;
    }

    //创建成员
    public function add_user($access_token,$data){
        $url = 'https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token='.$access_token;
        $result_status = http_curl($url,'POST',json_encode($data),'JSON');
        $result_status = json_decode($result_status);
        return $result_status;
    }

    //删除成员
    public function del_user($access_token,$userid){
        $url = 'https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token='.$access_token.'&userid='.$userid;
        $result_status = http_curl($url);
        $result_status = json_decode($result_status);
        return $result_status;
    }

    //创建部门
    public function add_department($access_token,$data){
        $url = 'https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token='.$access_token;
        $result_status = http_curl($url,'POST',json_encode($data),'JSON');
        $result_status = json_decode($result_status);
        return $result_status;
    }

    //获取部门列表
    public function get_department_lists($access_token){
        $url = 'https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token='.$access_token.'&id=ID';
        $department_lists = http_curl($url);
        $department_lists = json_decode($department_lists);
        return $department_lists;
    }

    //获取部门列表
    public function del_department($access_token,$departmentid){
        $url = 'https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token='.$access_token.'&id='.$departmentid;
        $department_ststus = http_curl($url);
        $department_ststus = json_decode($department_ststus);
        return $department_ststus;
    }

    //创建标签
    public function add_tags($access_token,$data){
        $url = 'https://qyapi.weixin.qq.com/cgi-bin/tag/create?access_token='.$access_token;
        $result_status = http_curl($url,'POST',json_encode($data),'JSON');
        $result_status = json_decode($result_status);
        return $result_status;
    }

    //获取tag列表
    public function get_tags_lists($access_token){
        $url = 'https://qyapi.weixin.qq.com/cgi-bin/tag/list?access_token='.$access_token;
        $tags_lists = http_curl($url);
        $tags_lists = json_decode($tags_lists);
        return $tags_lists;
    }
    //删除tag标签
    public function del_tag($access_token,$tagid){
        $url = 'https://qyapi.weixin.qq.com/cgi-bin/tag/delete?access_token='.$access_token.'&tagid='.$tagid;
        $tags_ststus = http_curl($url);
        $tags_ststus = json_decode($tags_ststus);
        return $tags_ststus;

    }



    //消息接口定义
    public function news_define($access_token,$data){
        $url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='.$access_token;
        $department_lists = http_curl($url,'POST',$data,'JSON');
        $department_lists = json_decode($department_lists);
        if($department_lists->errcode>0)return false;
        return $department_lists;
    }





}

